package com.example.appendixutilitydemo.controller;

import com.alibaba.excel.EasyExcel;
import com.example.appendixutilitydemo.controller.common.ResponseResult;
import com.example.appendixutilitydemo.dao.StudentDao;
import com.example.appendixutilitydemo.upload.StudentUploadDto;
import com.example.appendixutilitydemo.upload.StudentUploadListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @desc:
 * @author: Ping
 * @date: 2024/11/20 19:46
 */
@Controller
@RequestMapping("file")
public class FileController {
    /**
     * studentDao 存储学生数据
     */
    private StudentDao studentDao ;

    @PostMapping("excel-upload")
    @ResponseBody
    public ResponseResult<String> excelUpload(MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(), StudentUploadDto.class, new StudentUploadListener(studentDao)).sheet().doRead();
            return ResponseResult.ok("文件上传成功");
        } catch (IOException e) {
            return ResponseResult.error(HttpStatus.INTERNAL_SERVER_ERROR, "文件上传失败");
        }
    }






}
